<template>
  <div class="login_container">
    <div class="login">
      <h2>叩丁狼商品管理系统</h2>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            type="string"
            placeholder="请输入账号"
            autocomplete="off"
            size="large"
          >
            <template #prefix>
              <el-icon class="el-input__icon" :size="24" color="#454545"
                ><UserFilled
              /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            placeholder="请输入密码"
            autocomplete="off"
            size="large"
            :show-password="true"
            @keyup.enter="submitForm(ruleFormRef)"
          >
            <template #prefix>
              <el-icon class="el-input__icon" :size="24" color="#454545"
                ><Lock
              /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            v-shake="true"
            type="primary"
            @click="submitForm(ruleFormRef)"
            size="large"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { UserFilled, Lock } from "@element-plus/icons-vue";
import { loginAPI } from "./api";
import { useRouter } from "vue-router";

const router = useRouter();

const ruleFormRef = ref<FormInstance>();
// 校验密码非空
const validatePass = (rule: any, value: string, callback: any) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    if (ruleForm.password !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("password", () => null);
    }
    callback();
  }
};

const ruleForm = reactive({
  username: "",
  password: "",
});
// 校验规则
const rules = reactive({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ validator: validatePass, trigger: "blur" }],
});
// 登录按钮校验
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      // console.log("submit!");
      loginAPI(ruleForm).then((res) => {
        // console.log(res);
        if (res.code === 200) {
          // 登录
          // 1.存token
          localStorage.setItem("token", res.data.tokenHead + res.data.token);
          // 2.跳转
          router.push("/index");
          // 3.提示
          ElMessage.success("登录成功");
        }
      });
    } else {
      ElMessage.error("这得加钱！！");
      return false;
    }
  });
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2d3a4b;
  width: 100%;
  height: 100%;
  overflow: hidden;
  .login {
    width: 420px;
    color: #fff;
    text-align: center;
    // margin: 0 auto;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    :deep(.el-input__inner) {
      height: 50px;
    }
    :deep(.el-input__wrapper) {
      background-color: rgba(0, 0, 0, 0.1);
    }

    :deep(.el-button--primary) {
      width: 100%;
    }
    h2 {
      padding: 10px 0;
    }
  }
}
</style>
